<!--
 * @Description: 选择送修人
 * @Autor: 何兴家
 * @Date: 2021-01-19 15:11:53
 * @LastEditors: 何兴家
 * @LastEditTime: 2021-02-26 09:14:57
-->
<template>
  <div>
    <FilterTable
      ref="table"
      height="auto"
      :columns="columns"
      selectionType="single"
      :isColumnFilter="false"
      :dataSource="tableDataSource"
      :onSyncTableData="handleSyncTableData"
      :onRowSelectChange="handleTableRowSelect"
      :onColumnsChange="columns => (this.columns = columns)"
    >
    </FilterTable>

    <div class="dialogBottmView">
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { confirmAction, notifyAction, findFormItem, findTableItem } from '@/utils';

import { getVehicleInfo } from '@/api/customerRepair/customerInfo/ownerVehicleManagement';

export default {
  props: ['vin', 'ownerCode'],
  mixins: [dictionary],
  data() {
    this.selectTableRows = []; // 选中的table行项
    return {
      columns: this.createTableColumns(),
      tableDataSource: [], // table数据源
      syncTableData: [] // 同步的table数据
    };
  },
  mounted() {
    this.queryTableData();
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '客户编号',
          dataIndex: 'ownerNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系人',
          dataIndex: 'ownerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系人性别',
          dataIndex: 'gender',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(8012),
          dictItems: this.createDictList(8012)
        },
        {
          title: '联系人手机',
          dataIndex: 'mobile',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    // 同步table数据
    handleSyncTableData(list) {
      this.syncTableData = list;
    },
    // table行项选择变化
    handleTableRowSelect(list) {
      this.selectTableRows = list;
    },
    // 查询table数据
    async queryTableData() {
      this.$refs.table.START_LOADING();
      try {
        let res = await getVehicleInfo({
          linkOwnerNo: this.ownerCode,
          linkVin: this.vin,
          linkType: 'contact'
        });
        if (res.resultCode == 200 && res.data.rows.length) {
          this.tableDataSource = res.data.rows[0].linkOwnerList || [];
        }
      } catch (err) {
        console.log('查询送修人信息失败');
      } finally {
        this.$refs.table.STOP_LOADING();
      }
    },
    // 点击取消
    handleCancel() {
      this.$emit('close');
    },
    // 点击确定
    handleConfirm() {
      if (!this.selectTableRows.length) return notifyAction('请选择需要带回的送修人', 'warning');
      this.$emit('onConfirm', this.selectTableRows[0]);
    }
  }
};
</script>

<style scoped></style>
